package client.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import server.TCPServer;
import client.TCPClient;

public class VentanaACT extends JFrame {

	private String ip;
	private String user;
	private String pass;
	public boolean ipChecked = false;
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JPanel ipPanel = null;
	private JPanel infoPanel = null;
	private JPanel buttonPanel = null;
	public JButton connectButton = null;
	private JLabel infoLabel = null;
	private JLabel ipLabel = null;
	private JTextField ipTextField = null;
	private JPanel checkPanel = null;
	private JLabel checkLabel = null;
	private JButton exitButton = null;
	private TCPClient cliente;

	/**
	 * This is the default constructor
	 */
	public VentanaACT(TCPClient c) {
		super();
		cliente = c;
		setIconImage(Toolkit.getDefaultToolkit().getImage("images\\dron.png"));
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(393, 247);
		this.setContentPane(getJContentPane());
		this.setTitle("drones");
		this.setVisible(true);
	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridLayout gridLayout = new GridLayout();
			gridLayout.setRows(1);
			jContentPane = new JPanel();
			jContentPane.setLayout(gridLayout);
			jContentPane.add(getJPanel(), null);
		}
		return jContentPane;
	}

	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(getCheckPanel());
			jPanel.add(getIpPanel());
			jPanel.add(getInfoPanel());
			jPanel.add(getButtonPanel());
		}
		return jPanel;
	}

	private JPanel getIpPanel() {
		if (ipPanel == null) {
			ipLabel = new JLabel();
			ipLabel.setBounds(12, 8, 51, 25);
			ipLabel.setIcon(new ImageIcon("images\\ip.png"));
			ipLabel.setText("IP: ");
			ipPanel = new JPanel();
			ipPanel.setBorder(UIManager
					.getBorder("InternalFrame.optionDialogBorder"));
			ipPanel.setBounds(0, 40, 377, 47);
			ipPanel.setPreferredSize(new Dimension(150, 30));
			ipPanel.setLayout(null);
			ipPanel.add(ipLabel);
			ipPanel.add(getIpTextField());

			JLabel lblvacoLocalhost = new JLabel("(Vac\u00EDo = localhost)");
			lblvacoLocalhost.setBounds(234, 12, 108, 16);
			ipPanel.add(lblvacoLocalhost);
		}
		return ipPanel;
	}

	private JPanel getInfoPanel() {
		if (infoPanel == null) {
			infoPanel = new JPanel();
			infoPanel.setBorder(UIManager
					.getBorder("InternalFrame.optionDialogBorder"));
			infoPanel.setBounds(0, 99, 377, 47);
			infoPanel.setLayout(new GridBagLayout());
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.insets = new Insets(0, 0, 0, 5);
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			infoLabel = new JLabel();
			infoLabel.setHorizontalAlignment(SwingConstants.LEFT);
			infoLabel.setText("Bienvenido");
			infoLabel.setDisplayedMnemonic(KeyEvent.VK_UNDEFINED);
			infoPanel.add(infoLabel, gridBagConstraints);
		}
		return infoPanel;
	}

	private JPanel getButtonPanel() {
		if (buttonPanel == null) {
			buttonPanel = new JPanel();
			buttonPanel.setBorder(UIManager
					.getBorder("InternalFrame.optionDialogBorder"));
			buttonPanel.setBounds(0, 158, 377, 47);
			buttonPanel.setLayout(new FlowLayout());
			buttonPanel.add(getConnectButton(), null);
			buttonPanel.add(getExitButton(), null);
		}
		return buttonPanel;
	}

	private JButton getConnectButton() {
		if (connectButton == null) {
			connectButton = new JButton();
			connectButton.setPreferredSize(new Dimension(100, 25));
			connectButton.setText("Conectar");
			connectButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {

					if (ipTextField.getText().isEmpty()) {
						try {
							TCPServer.ipAct = InetAddress.getLocalHost()
									.getHostAddress();
						} catch (UnknownHostException e1) {
							e1.printStackTrace();
						}
					} else
						TCPServer.ipAct = ipTextField.getText();

					try {
						cliente.actualizar();
					} catch (IOException e) {
						e.printStackTrace();
					}
					close();
				}

			});
		}
		return connectButton;
	}

	private JTextField getIpTextField() {
		if (ipTextField == null) {
			ipTextField = new JTextField();
			ipTextField.setBounds(69, 10, 159, 21);
			ipTextField.setPreferredSize(new Dimension(130, 20));
		}
		return ipTextField;
	}

	private JPanel getCheckPanel() {
		if (checkPanel == null) {
			checkLabel = new JLabel();
			checkLabel.setBounds(170, 10, 53, 16);
			checkLabel.setText("Cliente");
			checkPanel = new JPanel();
			checkPanel.setBorder(UIManager
					.getBorder("InternalFrame.optionDialogBorder"));
			checkPanel.setBounds(0, 2, 377, 35);
			checkPanel.setBackground(new Color(135, 206, 235));
			checkPanel.setLayout(null);
			checkPanel.add(checkLabel);
		}
		return checkPanel;
	}

	private JButton getExitButton() {
		if (exitButton == null) {
			exitButton = new JButton();
			exitButton.setPreferredSize(new Dimension(100, 25));
			exitButton.setText("Exit");
		}
		exitButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				close();
			}
		});
		return exitButton;
	}

	public String getIp() {
		return ip;
	}

	public String getUser() {
		return user;
	}

	public String getPass() {
		return pass;
	}

	public void setInfo(String text) {
		this.infoLabel.setText(text);
		this.infoLabel.setVisible(true);
	}

	public void close() {
		this.setVisible(false);
	}
}